package com.gcxy.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gcxy.Vo.RanchVo;
import com.gcxy.conf.result.R;
import com.gcxy.entity.Ranch;
import com.gcxy.service.RanchService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jiang
 * @since 2023-12-12
 */
@RestController
@RequestMapping("/ranch")
@Api(tags = "牧场管理")
public class RanchController {


    @Autowired
    private RanchService ranchService;


    /**
     * 牧场列表
     * @return
     */
    @ApiOperation("牧场列表")
    @GetMapping("/getAll")
    public R list(){
        List<Ranch> list = ranchService.list();
        return R.Success(list);
    }

    /**
     * 新增
     */
    @ApiOperation("新增")
    @PostMapping("/add")
    public R add(@RequestBody Ranch ranch){
        if(!ranchService.getRanchName(ranch)){
            return R.Failed("新增失败，该牧场名已存在");
        }
        boolean result = ranchService.addRanch(ranch);
        if(result){
            return R.Success("新增成功");
        }
        return R.Failed("新增失败");
    }


    /**
     * 分页查询
     * @param ranchVo
     * @return
     */
    @ApiOperation("分页查询")
    @PostMapping("/page")
    public R  getRanchByPage(@RequestBody RanchVo ranchVo){
        Page<RanchVo> ranchByPage = ranchService.getRanchByPage(ranchVo);
        return R.Success(ranchByPage);
    }


    /**
     * 修改
     * @param ranch
     * @return
     */
    @ApiOperation("修改")
    @PostMapping("/update")
    public R update(@RequestBody Ranch ranch){
        if(!ranchService.getRanchName(ranch)){
            return R.Failed("修改失败，该牧场名已存在");
        }
        boolean result = ranchService.updateRanch(ranch);
        if(result){
            return R.Success("修改成功");
        }
        return R.Failed("修改失败");
    }


    /**
     *  修改启用禁用
     * @param ranch
     * @return
     */
    @PutMapping("/updateIsEnable")
    @ApiOperation("修改启用禁用")
    public R updateIsEnable(@RequestBody Ranch ranch){
        boolean result = ranchService.updateIsEnable(ranch);
        if(result){
            return R.Success("修改成功");
        }
        return R.Failed("修改失败");
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @GetMapping("getRanchById/{id}")
    @ApiOperation("根据id查询")
    public R getRanchById(@PathVariable("id") Integer id){
        Ranch ranch = ranchService.getById(id);
        if(ranch == null){
            return R.Failed("查询失败");
        }
        return R.Success(ranch);
    }


    /**
     * 获取牧场名称列表
     * @return
     */
    @GetMapping("/getRanchNameList")
    @ApiOperation("获取牧场名称列表")
    public R getRanchNameList(){
        List<String> list = ranchService.getRanchNameList();
        return R.Success(list);
    }

}

